﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.GeneralParameters;

namespace MyCommerce.Presentation.GeneralParameters.Presenters
{    
    public class CommonClassifierPresenter
    {

        private IViews.ICommonClassifier vManagedView;

        public CommonClassifierPresenter(IViews.ICommonClassifier managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            vManagedView = managedView;
            vManagedView.OnLoad += new EventHandler(vManagedView_OnLoad);
            vManagedView.OnEdit += new IViews.OnEntityEventHandler(vManagedView_OnEdit);
            vManagedView.OnDelete += new IViews.OnEntityEventHandler(vManagedView_OnDelete);
            vManagedView.OnSave += new IViews.OnSaveEventHandler(vManagedView_OnSave);

        }

        void vManagedView_OnSave(object sender, bool isNew)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            if (isNew)
            {                
                vCommonClassifierManager.SaveClassifier(vManagedView.objClassifier);
            }
            else
            {
                vCommonClassifierManager.UpdateClassifier(vManagedView.objClassifier);
            }
            CommonClassifierTypeManager vCommonClassifierTypeManager = new CommonClassifierTypeManager(true);
            vManagedView.colClassifierType = vCommonClassifierTypeManager.GetListClassifierTypeByIsSystem(false);
        }

        void vManagedView_OnDelete(object sender, long id)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            CommonClassifierTypeManager vCommonClassifierTypeManager = new CommonClassifierTypeManager(true);
            vCommonClassifierManager.DeleteClassifier(id);
            vManagedView.colClassifierType = vCommonClassifierTypeManager.GetListClassifierTypeByIsSystem(false);
        }

        void vManagedView_OnEdit(object sender, long id)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            vManagedView.objClassifier = vCommonClassifierManager.GetClassifierByID(id);
        }

        void vManagedView_OnLoad(object sender, EventArgs e)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            CommonClassifierTypeManager vCommonClassifierTypeManager = new CommonClassifierTypeManager();
            vManagedView.colClassifierType = vCommonClassifierTypeManager.GetListClassifierTypeByIsSystem(false);
        }
    }
}
